package com.musicalevents.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.musicalevents.dao.UserDAO;
import com.musicalevents.entity.User;

@Service
public class PasswordEncodingService {

	@Autowired
	private UserDAO userDAO;
	
	@Transactional
	public void encodeAndSaveUserPassword(int userId) {
		User u = userDAO.get(userId);
		
		if (u != null) {
			PasswordEncoder pwEncoder = new BCryptPasswordEncoder();
			String hashPwd = pwEncoder.encode(u.getPassword());
			u.setPassword(hashPwd);
			userDAO.update(u);
		}
	}
}
